package edu.gatech.oad.OTConfig;
import java.util.Random;

import edu.gatech.oad.OTRandom.*;


import javax.swing.JOptionPane;

public class JourneyController {
	private Journey journey;
	private Random rand;
	private EventController eventController = new EventController();
	private static final int STEADYMILES=15;
	private static final int STRENUOUSMILES=30;
	private static final int GRUELINGMILES=45;
	
	public static final int FILLING=5;
	public static final int MEAGER=3;
	public static final int BARE_BONES=1;
	
	private TrailNavigator trailNav;

	
	public JourneyController(){
		rand = new Random();
		journey = new Journey();
		trailNav = new TrailNavigator();
	}
	
	public void proceed(){
		updateDistance();
		updateRations();
	}
	

	public void updateDistance(){
		if (Wagon.getPace()==Pace.Steady){
			journey.updateMiles(STEADYMILES);
			trailNav.updateMilesToNext(STEADYMILES);
		}
		else if (Wagon.getPace()==Pace.Strenuous){
			journey.updateMiles(STRENUOUSMILES);
			trailNav.updateMilesToNext(STRENUOUSMILES);
		}
		else{
			journey.updateMiles(GRUELINGMILES);
			trailNav.updateMilesToNext(GRUELINGMILES);
		}
		
		if(trailNav.getMilesToNext() <= 0){
			journey.updateMiles(trailNav.getMilesToNext());
			trailNav.goToNextLocation();
			if(trailNav.getIsDestination()){
				JOptionPane.showMessageDialog(null, "You have arrived at " + trailNav.getLocation() + "!");
				if(trailNav.getLocation().equals("Oregon City")){
					JOptionPane.showMessageDialog(null, "You have won the game! Congratulations! Press OK to exit.");
					System.exit(0);
				}
			}
		}
	}
	
	public void updateRations(){
		if(!trailNav.getIsDestination()){
			if (Wagon.getRation().equals("Filling")){
				Wagon.getInventoryController().getWagonInventory().remove(Inventory.FOOD, FILLING);
			}
			else if (Wagon.getRation().equals("Meager")){
				Wagon.getInventoryController().getWagonInventory().remove(Inventory.FOOD, MEAGER);
			}
			else{
				Wagon.getInventoryController().getWagonInventory().remove(Inventory.FOOD, BARE_BONES);
			}
		}
	}
	
	
	public void rest(){
		updateRations();
		journey.rest();
	}
	
	
	public void riverCrossing(String input) {
		 
			
			// If they want to caulk the river
			 if(input.equals("Caulk")){
				 System.out.println("In river crossing, caulk");
				eventController.riverEvent();
			 }
			// If they want to ford the river
			 else if(input.equals("Ford")){
				if(rand.nextInt(2)==0){
					System.out.println("In river crossing, ford");
					eventController.riverEvent();
				}
				else{
					JOptionPane.showMessageDialog(null, "You have successfully crossed the river!");
				}
			 }

		}
	
	public void randomEvent() {
		eventController.generateEvent();
	}
	
//	public Boolean checkForStore(){
//		
//	}
//	
//	public String checkTrail(){
//		if(journey.getTrailConditions().equals(Trail.CLEAR)){
//			
//		}
//		else if(journey.getTrailConditions().equals(Trail.ROCKY)){
//			
//		}
//		else if(journey.getTrailConditions().equals(Trail.BUMPY)){
//			
//		}
//	}
//	
//	public String checkWeather(){
//		if(journey.getWeatherConditions().equals(Weather.SUNNY)){
//			
//		}
//		else if(journey.getWeatherConditions().equals(Weather.RAINY)){
//			
//		}
//		else if(journey.getWeatherConditions().equals(Weather.STORMY)){
//			
//		}
//		else if(journey.getWeatherConditions().equals(Weather.DUSTY)){
//	
//		}
//	}
	
	public Journey getJourney() {
		return journey;
	}
	
	public TrailNavigator getTrailNavigator(){
		return trailNav;
	}
}
